package com.smark.sbrabbitmqfanoutproducer.mq;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

/**
 * @author smark
 */
@Component
public class WorkProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    //AmqpTemplate是一个接口，最终找到实现类也就是：RabbitTemplate
//    @Autowired
//    private AmqpTemplate template;

    //1、定义交换机

    private String exchangeName="";

    //2、路由key
    private String queueName="order.queue";

    public WorkProducer() {
    }

    /**
     * 模拟发送订单数据
     */
    public void sendMessages(int i, Channel channel) throws Exception {
        String orderId= UUID.randomUUID().toString();
        String message = i+" ===>你的订单信息是："+orderId+",日期是："+new Date();

        //限流，一个一个接收
//        channel.basicQos(1,false);
        //发送消息
        rabbitTemplate.convertAndSend(exchangeName,queueName,message);
    }


}
